import '../css/style.scss';
import {createewm ,loginStatus,log,sendEmailCode,user} from './urlconfig/config.js';
var interval;
var token;
function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
}
$(function(){
    $(".login-tab .tab-span").click(function () {
        var id = $(this).attr("data-id");
        $(this).siblings("span").removeClass("active");
        $(this).addClass("active");
        if(id > 0){
            $('.login-show').css('display','none');
            $("#tab-" + id).css('display','block');
        }
        if($(this).hasClass("tab-email")){
            $(".email-input").focus();
            tab('emailClick');
            clearInterval(interval);
            clearTimeout(sttime);
        }else if($(this).hasClass("tab-weixin")){
            status()
            tab('wxClick');

        }

    });
});
login();
/*获取验证码*/
var wait=60;
function time(o){
    if (wait==0) {
        o.removeAttribute("disabled");
        o.innerHTML="发送验证码";
        o.style.backgroundColor="#a1d5ff";
        wait=60;
    }else{
        o.setAttribute("disabled", true);
        o.innerHTML=wait+"秒";
        o.style.backgroundColor="#a1d5ff";
        wait--;
        console.log(wait);
        setTimeout(function(){
            time(o)
        },1000)
    }
}


    function checkemail(email) {
        var remail= /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (!email) {
            $(".login-title").html("");
             $(".login-err").html("邮箱不能为空");
            return false;
        }
        if (!remail.test(email)) {
            $(".login-title").html("");
            $(".login-err").html("邮箱格式不正确");
            return false;
        }
        return true;
    }

    $("#btn").click(function () {
        var email=$(".email-input").val();
        if (!checkemail(email)) {
            tab('emailFalseCodeClick');
            return false;
        }else{
            $(".login-err").html("");
            var disabled = $("#btn").attr("disabled");
            if(disabled){
               return false;
            }else{
                tab('emailTrueCodeClick');
                sendEmail();
                time(this);
            }
        }
    });
/*发送验证码*/
function sendEmail(){
    var email=$(".email-input").val();
    $.ajax({
        type:'post',
        url:sendEmailCode,
        dataType:'json',
        data:{
            email:email
        },
        success:function (res){
            if(res.code==200){
                $(".login-err").html("");
                console.log(res.msg)
                tab('getEmailCheckCodeSuccess');
            }else{
                $(".login-err").html(res.msg);
                tab('getEmailCheckCodeFaile');
            }

        },
        error:function (){

        }

    })
}
/*邮箱登陆*/
function loginEmail(){
    var email=$(".email-input").val();
    var code=$(".code-input").val();
    if (!checkemail(email)) {
        return false;
    }
    if(code.length==0){
        $(".login-err").html("验证码不能为空");
        return false;
    }
    $.ajax({
        type:'post',
        url:user,
        dataType:'json',
        data:{
            username:email,
            code:code
        },
        success:function (res){
            if(res.code==200){
                $(".login-err").html("");
                token=res.data.token;
                localStorage.setItem("token",token);
                tab('emailCodeTrueLoginClick');
                tab("emailLoginSucceed");
                window.location.href="/pay.html"
            }
            else{
                console.log(res.msg);
                $(".login-err").html(res.msg);
                tab("emailCodeFalseLoginClick");
            }

        },
        error:function (){

        }

    })
}
$(".login-sub").click(function (){
    loginEmail()
});

$(".email-input").bind("input propertychange",function(event){
    var $btn=$(".btn-button");
    if($(".email-input").val() ==""){
        $btn.css("background-color","#a1d5ff");
        $btn.attr("disabled","disabled");
        $(".login-title").html("邮箱验证后，接收正版激活码");
        $(".login-err").html("");
    }else{
        $btn.css("background-color","#0078d7");
        $btn.attr("disabled",false);
        $(".login-err").html("");
    }
});
$(".code-input").bind("input propertychange",function(event){
    var $btn=$(".login-sub");
    if($(".email-input").val() ==""){
        $btn.css("background-color","#a1d5ff");
        $btn.attr("disabled","disabled");
    }else{
        $btn.css("background-color","#0078d7");
        $btn.attr("disabled",false);
        $(".login-err").html("");
    }
});
var url = document.location.toString();

    if (url.indexOf("?") != -1) {
        var edition=GetUrlParam("edition");
        var channel=GetUrlParam("channel");
        var cpuid=GetUrlParam("cpuid");
        var sys=GetUrlParam("sys");
        var storage=window.localStorage;
        storage.setItem("channel",channel);
        storage.setItem("cpuid",cpuid);
        storage.setItem("edition",edition);
        storage.setItem("sys",sys);
    }else{
        var storage=window.localStorage;
        storage.setItem("channel",'plat_main');
        storage.setItem("cpuid",'BFEBFBFF0001067A002100A561EB');
        storage.setItem("edition",'WX_3.6.1.1');
        storage.setItem("sys",'win10');
    }



var scene_id;
/*请求二维码*/


function login(){
    $.ajax({
        type:'post',
        url:createewm,
        dataType:'json',
        success:function (res){
            if(res.code === 200){
                settime();
                $(".email-input").focus();
                $(".jihuo-bottom").empty();
                $(".login_loading").hide();
                var $login= '<img src="'+ res.data.url + '"   class="login-ewm" ondragstart="return false;">'+
                    '<p>微信扫码登录</p>';
                $(".jihuo-bottom").append($login);
                scene_id =res.data.scene_id;
                if(scene_id !=null){
                    status()
                }
             }
        },
        error:function (err){

        }
    })
}
function loginSuccess(name){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        async:false,
        success:function(res) {
            if(res.code==200){
                console.log(res.msg);
            }
        },
        error:function(err) {
            console.log(err)
        }
    });
}

function status(){
    interval = setInterval(function () {
        $.ajax({
                type:'post',
                url:loginStatus,
                dataType:'json',
                data:{
                    scene_id:scene_id
                },
                success:function (res){
                    if(res.code === 200){
                        loginSuccess('QrCodeLoginSucceed');
                        token=res.data.token;
                        localStorage.setItem("token",token);
                        clearInterval(interval);
                        window.location.href="/pay.html";
                    }

                },
            error:function (err){
                console.log(err)
            }
        })
    },2000)

}
var countdown=900;
var sttime;
function settime() {
    if (countdown == 0) {
        countdown = 900;
        $(".login-again").show();
        clearInterval(interval);
    } else {
        countdown--;
       sttime = setTimeout(function() {
            settime();
        },1000)
    }
}

/*重新请求二维码*/
$(".login-again").click(function (){
    $(this).hide();
    $(".login_loading").show();
    login();
});


/*关闭按钮日志*/
function close(name){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        success:function(res) {
            if(res.code==200){
                console.log(res.msg)

            }
        },
        error:function(err) {
            console.log(err)
        }
    });
}

/*页面显示日志*/
var main;
if($("#fagepage").length>0){
    main=$("#fagepage").val();
}

$(function (){
    $.ajax({
        type:'GET',
        url:log+ main+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        success:function(res) {
            if(res.code==200){
                console.log(res.msg)

            }
        },
        error:function(err) {
            console.log(err)
        }
    });
});
$(".mall-close").click(function () {
    var btnlog=$(this).attr('data-log');
    var int=$(this).attr('data-into');
    close(btnlog);
    if('Widget' in window){
        Widget.slotCloseWindow(int)
    }
});

/*日志*/
function tab(name){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        async:false,
        success:function(res) {
            if(res.code==200){
                console.log(res.msg)
            }
        },
        error:function(err) {
            console.log(err)
        }
    });
}

document.oncontextmenu = function(){
    return false;
};
$(document).keydown(function(event){
    //屏蔽 Alt+ 方向键 ←
    //屏蔽 Alt+ 方向键 →
    if ((event.altKey)&&((event.keyCode==37)||(event.keyCode==39)))
    {
        event.returnValue=false;
        return false;
    }
    //屏蔽退格删除键
    //if(event.keyCode==8){
    //    return false;
    //}
    //屏蔽F5刷新键
    if(event.keyCode==116){
        return false;
    }
    //屏蔽alt+R
    if((event.ctrlKey) && (event.keyCode==82)){
        return false;
    }
});
var img=$("img");
img.on("contextmenu",function(){return false;});
img.on("dragstart",function(){return false;});
